#!/usr/bin/ruby
$VERBOSE = nil

usage = "./convert-2-png.rb message.meta message.body\n"

if(ARGV.size != 2)
  puts usage
  exit
end

metaname = ARGV[0]
bodyname = ARGV[1]

file = File.new(metaname, 'r')

pairs = file.readlines()

subj = ""
0.upto(pairs.size() - 1) {|x|
  subj = pairs[x].split().at(1)
  if(subj == "convert-2-png")
    break
  end
}
if(subj != "convert-2-png")
  exit
end

outfile = ""
0.upto(pairs.size() - 1) {|x|
  outfile = pairs[x].split().at(1)
  if(subj == "outfile")
    break
  end
}

%x[convert #{bodyname} #{outfile}]
#jpgsize = File.size(outfile)

#generate new header/body, send to stdout
header = "source convert-2-png\
        \ntarget png-sink\
        \noutfile #{outfile}\n"
headerfile = File.new("#{outfile}.meta", "w+")
headerfile.write(header)
headerfile.close()

#call smq put
r = %x[#{ENV['SMQ_MANAGER_BIN_PATH']}/smq_put relay #{outfile}.meta #{outfile}]

#make sure put succeeds

#clean up temporary files
#%x[rm -f #{outfile} #{headerfile}]
